/*___________________________________________________________________________________________添加航节*/

$(".add").click(function(){
	var No = $(".nodeMessDetail").length+1
	var str = "";
	str+="<div class='nodeMessDetail f868686'>"+
			"<div class=floor>"+No+"</div>"+
			"<div class=floor>"+
				"<p class=midTop>出发<input uppercase class='dep autocomplete' type=text placeholder=机场 /><span style='color:red;font-size: 16px;'>*</span>"+
					" <input uppercase class='terminal depTerminal' type='text' placeholder=航站楼 />"+
					" 时间<input class='depTime datespicker' onfocus=WdatePicker({dateFmt:'HH:mm'})  type='text'/><span style='color:red;font-size: 16px;margin-right:20px;'>*</span>"+
					" 日期变更  <select disabled class=depDc>"+
						     	"<option value=0>0</option>"+
						     	"<option value=-1>-1</option>"+
						     	"<option value=1>1</option>"+
						        "<option value=2>2</option>"+
						     	"<option value=3>3</option>"+
						     	"<option value=4>4</option>"+
						     	"<option value=5>5</option>"+
						     	"<option value=6>6</option>"+
						    " </select>" +
					"<span style='color:red;font-size: 16px;'>*</span>"+
				"</p>"+
				"<p class='midTop'>到达<input uppercase class='arr autocomplete' type='text' placeholder=机场 /><span style='color:red;font-size: 16px;'>*</span>"+
					" <input uppercase class='terminal arrTerminal' type='text' placeholder=航站楼 />"+
					" 时间<input class='arrTime datespicker' type='text' onfocus=WdatePicker({dateFmt:'HH:mm'}) /><span style='color:red;font-size: 16px;margin-right:20px;'>*</span>"+
					" 日期变更 <select class=arrDc>"+
						     	"<option value=0>0</option>"+
						     	"<option value=-1>-1</option>"+
						     	"<option value=1>1</option>"+
						        "<option value=2>2</option>"+
						     	"<option value=3>3</option>"+
						     	"<option value=4>4</option>"+
						     	"<option value=5>5</option>"+
						     	"<option value=6>6</option>"+
						    " </select>" +
					"<span style='color:red;font-size: 16px;'>*</span>"+
				"</p>"+
			"</div>"+			
		 	"<div class='floor'><span class='delete'>删除</span></div>"+
		 "</div>"
		 
	$(".nodeMess").append(str)
	$('.autocomplete').autocomplete({
	    lookup: autocompleteData,
	    autoSelectFirst:true,
	    onSelect: function (suggestion) {		   
	    }
	});
})

/*_________________________________________________________________________删除功能*/
$('.nodeMess').delegate(".delete","click",function(){
	var index = $(this).parent().parent().index()
	if($(".nodeMessDetail").length>1){		
		$(".nodeMessDetail").eq(index).remove()		
	}
})


/*_________________________________________________________________________自动计算*/
document.onkeyup = function(){
	var total = ($(".fseat").val()-0)+($(".cseat").val()-0)+($(".yseat").val()-0)
	$(".totalseat").val(total)
}
/*-________________________________________________________________________班期与隔天的处理————————*/
$(".sche2").prop("disabled",true);
$(".valide input[type=radio]").click(function(){
	if($(".chooseSchedule").is(":checked")){//班期选中
		$(".sche2").val("");
		$(".sche2").prop("disabled",true);
		$(".sche1").prop("disabled",false)
	}
	
	if($(".chooseIntval").is(":checked")){//隔天选中
		$(".sche1").val("");
		$(".sche1").prop("disabled",true);
		$(".sche2").prop("disabled",false);
		
	}
})


/*-________________________________________________________________________确认按钮————————*/
$(".sureButton").click(function(){	
	var message = {};
	message.airlineCode = ($(".airlineCode").val()).toUpperCase();
	message.pm = $(".pm").val();
	message.status = "U";
	message.startDate = $(".startDate").val();
	message.endDate = $(".endDate").val();
	message.aircraft = $(".aircraft").val();
	message.fseat = $(".fseat").val();
	message.cseat = $(".cseat").val();
	message.yseat = $(".yseat").val();	
	message.schIndctor = $("input[type='radio']:checked").val();
	message.remarks = $(".remarks").val();
	message.infantLimit = $(".infantLimit").val();
	if($(".checkCabinRank").is(":checked")){
		message.multiCabin = true;
	}else{
		message.multiCabin = false;
	}
	message.cabinSeq = $(".cabinRankVal").val();
	message.skStatus = $(".skStatus").val();
	if(message.schIndctor=="S"){
		message.schedule = $(".sche1").val();		
	}
	if(message.schIndctor=="I"){
		message.intval = $(".sche2").val();		
	}
/*	message.fluggage = $(".fbag").val();
	message.cluggage = $(".cbag").val();
	message.yluggage = $(".ybag").val();*/
	message.legs = [];
	/*以下为文本输入框做验证提示（不能为空）--begin--*/
	//1、航线编号不能为空
	if($(".airlineCode").val()==""||$(".airlineCode").val()==undefined||$(".airlineCode").val()==null){
		layer.msg("航线编号不能为空",{icon: 2,time:1000})
			return false;	
	}else if(!/^[0-9a-zA-Z]{1,24}$/.test($(".airlineCode").val())){
		layer.msg("航班编号必须为数字或者字母，长度不超过24位",{icon: 2,time:1500})
		return false;
	}
	//婴儿限制数
	if($(".infantLimit").val()==""){
		
	}else{
		if(!/^[0-9]{1,2}$/.test($(".infantLimit").val())){
			layer.msg("婴儿限制数为不超过2位的数字",{icon: 2,time:1000})
			return false; 
		}
	}
	
	
	
	//2、开始日期不能为空
	if($(".startDate").val()==""||$(".startDate").val()==undefined||$(".startDate").val()==null){
		layer.msg("开始日期不能为空",{icon: 2,time:1000})
		return false; 
		
	}
	//3、结束日期不能为空
	if($(".endDate").val()==""||$(".endDate").val()==undefined||$(".endDate").val()==null){
		layer.msg("结束日期不能为空",{icon: 2,time:1000})
		return false; 
	}
	//4、班期不能为空
	if($(".chooseSchedule").is(":checked")){
		if($(".sche1").val()==""){
			layer.msg("班期不能为空",{icon: 2,time:1000})
			return false;
		}else if(!/^[1-7]+$/.test($(".sche1").val())){
			layer.msg("班期必须为1-7之间的数字",{icon: 2,time:1000})
			return false;
		}
	}
	//隔天输入不能为空
	if($(".chooseIntval").is(":checked")){
		if($(".sche2").val()==""){
			layer.msg("隔天不能为空",{icon: 2,time:1000})
			return false;
		}else if(!/^[1-7]$/.test($(".sche2").val())){
			layer.msg("隔天必须为1-7之间的数字",{icon: 2,time:1000})
			return false;
		}
	}
	
	//5、航班机型不能为空
	if($(".aircraft").val()==""||$(".aircraft").val()==undefined||$(".aircraft").val()==null){
		layer.msg("航班机型不能为空",{icon: 2,time:1000})
		return false; 
	}
	
	if($(".fseat").val()==""||$(".fseat").val()==undefined||$(".fseat").val()==null){
		layer.msg("头等舱座位数不能为空",{icon: 2,time:1000})
		return false; 
	}else if(!/^[0-9]{1,3}$/.test($(".fseat").val())){
		layer.msg("头等舱座位数必须为不超过3位的数字",{icon: 2,time:1000})
		return false; 
	}
	
	if($(".cseat").val()==""||$(".cseat").val()==undefined||$(".cseat").val()==null){
		layer.msg("商务舱座位数不能为空",{icon: 2,time:1000})
		return false; 
	}else if(!/^[0-9]{1,3}$/.test($(".cseat").val())){
		layer.msg("商务舱座位数必须为不超过3位的数字",{icon: 2,time:1000})
		return false; 
	}
	
	if($(".yseat").val()==""||$(".yseat").val()==undefined||$(".yseat").val()==null){
		layer.msg("经济舱座位数不能为空",{icon: 2,time:1000})
		return false; 
	}else if(!/^[0-9]{1,3}$/.test($(".yseat").val())){
		layer.msg("经济舱座位数必须为不超过3位的数字",{icon: 2,time:1000})
		return false; 
	}
	
	var len_dep=$(".dep").length;
	for(var i=0;i<len_dep;i++){
		//6、出发机场不能为空
		if($(".dep")[i].value==""||$(".dep")[i].value==undefined||$(".dep")[i].value==null){
			layer.msg("出发机场不能为空",{icon: 2,time:1000})
			return false; 
		}
		//7、到达机场不能为空
		if($(".arr")[i].value==""||$(".arr")[i].value==undefined||$(".arr")[i].value==null){
			layer.msg("到达机场不能为空",{icon: 2,time:1000})
			return false; 
		}
		//10、出发航站楼若不为空则不超过5位
		if($(".depTerminal")[i].value!=""){
			 if(!/^[0-9a-zA-Z]{1,5}$/.test($(".depTerminal")[i].value)){
					layer.msg("出发航站楼的长度不能超过5位",{icon: 2,time:1000})
					return false; 
				}
		}
		//11、到达航站楼若不为空则不超过5位
		if($(".arrTerminal")[i].value!=""){
			 if(!/^[0-9a-zA-Z]{1,5}$/.test($(".arrTerminal")[i].value)){
					layer.msg("到达航站楼的长度不能超过5位",{icon: 2,time:1000})
					return false; 
				}
		}
		//8、出发时间不能为空
		if($(".depTime")[i].value==""||$(".depTime")[i].value==undefined||$(".depTime")[i].value==null){
			layer.msg("出发时间不能为空",{icon: 2,time:1000})
			return false; 
		}
		//9、到达时间不能为空
		if($(".arrTime")[i].value==""||$(".arrTime")[i].value==undefined||$(".arrTime")[i].value==null){
			layer.msg("到达时间不能为空",{icon: 2,time:1000})
			return false; 
		}
	
		//12 承运航班验证若不为空则不超过7位
		/*if($(".OC")[i].value!=""){
			 if(!/^[0-9a-zA-Z]{1,7}$/.test($(".OC")[i].value)){
					layer.msg("承运航班的长度不能超过7位",{icon: 2,time:1000})
					return false; 
				}
		}*/
		
	}
	
	
	/*验证信息  --end--*/
	//console.log(/\((.)+?\)/.test($(".dep").val()))//判断是否含有小括号  是
	//console.log($(".dep").val().slice(-5).split("(")[1].split(")")[0]) 获取小括号内的值
	
	var length = $(".nodeMessDetail").length;
	for(var i=0;i<length;i++){
		var json={};
		if(/\((.)+?\)/.test($(".dep").eq(i).val())){//获取出发机场三字码
			json.dep = $(".dep").eq(i).val().slice(-5).split("(")[1].split(")")[0];
		}else{
			json.dep = ($(".dep").eq(i).val()).toUpperCase();
		}
		if(/\((.)+?\)/.test($(".arr").eq(i).val())){//获取到达机场三字码
			json.arr = $(".arr").eq(i).val().slice(-5).split("(")[1].split(")")[0];
		}else{
			json.arr = ($(".arr").eq(i).val()).toUpperCase()
		}
		//json.dep = $(".dep").eq(i).val()
		//json.arr = $(".arr").eq(i).val()
		json.depTerminal = ($(".depTerminal").eq(i).val()).toUpperCase()
		json.arrTerminal = ($(".arrTerminal").eq(i).val()).toUpperCase()
		json.depTime = $(".depTime").eq(i).val()
		json.depDc = $(".depDc").eq(i).val()
		json.arrTime = $(".arrTime").eq(i).val()
		json.arrDc = $(".arrDc").eq(i).val()
		//json.OC = $(".OC").eq(i).val()
		message.legs.push(json)
	}

	layer.confirm('是否直接生成航班？', {
		btn: ['确定','暂不'] //按钮
	}, 
	function(){   
		message.intoEffect = true;
		layer.msg('生成中...', {icon: 16,shade: 0.01});
		$.ajax({
			type:"post",
			url:"../flightSchedule/api/create?temp="+Math.random(),
			contentType:'application/json;charset=utf-8',
			data:JSON.stringify(message),
			async:true,
			success:function(data){
				data = JSON.parse(data)
				if(data.status=="1001"){
					layer.msg("创建航班成功，已生效！",{icon:1},function(){
						closeWin()
					})
				}else{
					//layer.msg("创建航班失败！"+data.message,{icon:2})
					//失败的弹出框
					
					layer.confirm(data.message, {
						title:['创建航班失败','background-color:#00aa98;color:#fff'],
					   	btn: ['确定'] //按钮
					}, function(){
						layer.closeAll('dialog');
					});
				}
			}			
		});		
	}, 
	function(){                        
		message.intoEffect = false;
		layer.msg('新建中...', {icon: 16,shade: 0.01});
		$.ajax({
			type:"post",
			url:"../flightSchedule/api/create?temp="+Math.random(),
			contentType:'application/json;charset=utf-8',
			data:JSON.stringify(message),
			async:true,
			success:function(data){
				data = JSON.parse(data)
				console.log(data)
				if(data.status=="1001"){
					layer.msg("创建航班成功!",{icon:1},function(){
						closeWin()
					})
				}else{
					//layer.msg("创建航班失败!"+data.message,{icon:2})
					layer.confirm(data.message, {
						title:['创建航班失败','background-color:#00aa98;color:#fff'],
					   	btn: ['确定'] //按钮
					}, function(){
						layer.closeAll('dialog');
					});
					
				}
			}			
		});			
	})
})
//____________________________________________________________________________多等级舱位
		$(".checkCabinRank").click(function(){
			if($(this).is(":checked")){
				var airlineCode = $(".airlineCode").val();
				if(airlineCode==""){
					layer.msg("请先输入航班号",{icon:2});
					$(this).attr("checked",false);
				}else{
					var cabinSeq = (airlineCode.substring(0,2)).toUpperCase();
					$.ajax({
						type:"get",
						url:"../flightSchedule/"+cabinSeq+"/cabinSeq?temp="+Math.random(),
						dataType:"json",
						success:function(data){	
							if(data.status==1001){
								$(".cabinRankVal").val(data.data);
							}else{
								layer.alert(data.message);
								$(this).attr("checked",false);
							}
						}
					})
				}				
			}else{
				$(".cabinRankVal").val("F/C/Y");
			}			
		})

//____________________________________________________________________________机场联想
		var autocompleteData=[];
		$.ajax({
			type:"get",
			url:"../Airapt?pageSize=100000&pageNum=1&temp="+Math.random(),
			dataType:"json",
			success:function(data){	
				autocompleteData = data.data.list; 
				$('.autocomplete').autocomplete({
				    lookup: autocompleteData,
				    autoSelectFirst:true,
				    onSelect: function (suggestion) {		   
				    }
				});
			}
		})
//____________________________________________________________________________机型联想
		var aircftAutocompleteData=[];
		$.ajax({
			type:"get",
			url:"../Aircft?pageSize=100000&pageNum=1&temp="+Math.random(),
			dataType:"json",
			success:function(data){	
				aircftAutocompleteData = data.data.list; 
				$('#aircraft').autocomplete({
				    lookup: aircftAutocompleteData,
				    autoSelectFirst:true,
				    onSelect: function (suggestion) {		   
				    }
				});
			}
		})
		